class IncrementalBackupEngine:
    def __init__(self, graph_client, storage, checkpoint_manager):
        self.client = graph_client
        self.storage = storage
        self.checkpoints = checkpoint_manager
        
    def backup_mailbox(self, user_id: str):
        """执行增量备份"""
        last_token = self.checkpoints.get_checkpoint(user_id)
        delta_messages = self._get_delta_changes(user_id, last_token)
        
        if delta_messages:
            self._process_delta(delta_messages)
            new_token = delta_messages[-1]["@delta.token"]
            self.checkpoints.save_checkpoint(user_id, new_token)
        
    def _get_delta_changes(self, user_id: str, delta_token: str = None):
        """获取增量变更"""
        endpoint = f"users/{user_id}/messages/delta"
        params = {"$deltatoken": delta_token} if delta_token else None
        return list(self.client.paginate(endpoint, params))
    
    def _process_delta(self, messages):
        """处理变更数据"""
        creates = [msg for msg in messages if not msg.get("@removed")]
        deletes = [msg for msg in messages if msg.get("@removed")]
        
        # 处理新增/修改项
        self.storage.store_changes(creates)
        # 处理删除项
        self.storage.mark_deletions(deletes)